//后端数据
var list = [];
import Mock from 'mockjs'
import { param2Obj } from '../utils/url';
for (var i = 0; i < 100; i++) {
    list.push({
        id: Mock.mock('@guid()'),
        username: Mock.mock('@cname()'),
        address: Mock.mock('@county(true)'),
        type: Mock.mock('@natural(0, 3)'),
        date: Mock.mock('@datetime()'),
        income: Mock.mock('@natural(10000, 20000)'),
        pay: Mock.mock('@natural(200, 1000)'),
        money: Mock.mock('@natural(5000, 10000)')
    })
}

export default {
    gethomedata: config => {
        var { keyword, page, pageSize } = param2Obj(config.url)
        var res = list.filter(item => item.username.indexOf(keyword) != -1)
            // page  pageSize 是做分页的一个关键
            // page=1 pageSize=20      下标0----下标19 
            // page=2 pageSize=20      下标20----下标39 
            // page=3 pageSize=20      下标40----下标59 
            //  ( page-1 )* pageSize <= index < page * pageSize   0<=下标<20
        var resLimit = res.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })
        return {
            code: 200,
            msg: '请求成功',
            data: resLimit,
            total: list.length
        }
    },
    deleteHomeByid: config => {
        // console.log(config);
        var { id } = JSON.parse(config.body);
        var res = list.filter(item => item.id != id);
        list = res;
        return {
            code: 200,
            msg: '删除成功',
            data: null
        }
    },
    deleteMoreByids: config => {
        var { ids } = JSON.parse(config.body);
        // console.log(ids)
        var res = list.filter(item => !ids.includes(item.id))
        list = res;
        return {
            code: 200,
            msg: '批量删除成功',
            data: null
        }
    },
    addData: config => {
        var { username, address, income, pay, money, type } = JSON.parse(config.body);
        var obj = {
            username: username,
            address: address,
            income: income,
            pay: pay,
            money: money,
            type: type,
            id: Mock.mock('@guid()'),
            date: Mock.mock('@datetime()')
        }
        list.unshift(obj);
        return {
            code: 200,
            msg: '添加成功',
            data: null
        }
    },
    updateData: config => {
        const { id, username, address, income, pay, money, type } = JSON.parse(config.body);
        list.forEach(item => {
            if (item.id == id) {
                item.username = username,
                    item.address = address,
                    item.income = income,
                    item.pay = pay,
                    item.money = money,
                    item.type = type
            }
        })
        return {
            code: 200,
            msg: '修改成功',
            data: null
        }
    }
}